package gui;

import gui.AbstractViewPane.ViewMode;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SpringLayout;

import business.StatutEnseignantBL;
import business.TypeHeureBL;

public class PanelStatutUIConsulter extends JFrame {
	
	
	private ViewMode currentViewMode;
	protected JFrame masterClass = this;
	
	//champs du formulaire
	private JPanel general;
    private JButton save;
    private JButton annuler;
	JTextField textFieldNom;
	private JSpinner spinEquiv;

  //variables d'affichage du formulaire
    private int id = -1;
    private String nom = "";
    int equiv = 200;
    
    private PanelStatutUI panelSource;
    
	//methodes de récupperation des éléments des champs
	public int getId(){
		return id;
	}
	public String getNom(){
		return textFieldNom.getText();
	}
	
	public int getEquiv() {
		return Integer.parseInt(spinEquiv.getValue().toString());
	}
	
	
	public void drawCommandBox(JPanel commandBox) {
        JPanel buttonPanel = new JPanel(new BorderLayout());
        buttonPanel.add(new JSeparator(), BorderLayout.PAGE_START);
        buttonPanel.add(commandBox, BorderLayout.LINE_END);
        
        general.add(buttonPanel, BorderLayout.SOUTH);
	}
	
	public PanelStatutUIConsulter(ViewMode v, int idStatutEnseignant, PanelStatutUI panelS) {
		
		StatutEnseignantBL statutEnseignantBL = new StatutEnseignantBL();
		currentViewMode = v;
		panelSource = panelS;
		
    	setSize(500, 300);
		setTitle("Gestion des statuts d'enseignant");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		general = new JPanel();
		general.setLayout(new BorderLayout());
		
		general.setBorder(BorderFactory.createCompoundBorder(
		BorderFactory.createTitledBorder(getTitle()),
		BorderFactory.createEmptyBorder(5,5,5,5)));

		
		// si l'id du noeud envoyé est -1, on est en mode ajout, les champs
		// seront donc vides
		if (idStatutEnseignant == -1) {
			nom = "";
			id = -1;
		} else {
			// si on est en consultation ou modification (récuperation des
			// elements en base)
			id = idStatutEnseignant;
			statutEnseignantBL = new StatutEnseignantBL(id);
			nom = statutEnseignantBL.getNomStatutEns();
		}
		
		
        //Create and populate the panel.
        JPanel p = new JPanel(new SpringLayout());
        
        JLabel l = new JLabel("Nom:", JLabel.TRAILING);
        p.add(l);
        textFieldNom = new JTextField (nom);
        l.setLabelFor(textFieldNom);
        p.add(textFieldNom);      
        
        l = new JLabel("Heures équivalent TD à effectuer:", JLabel.TRAILING);
        p.add(l);
        SpinnerModel sm = new SpinnerNumberModel(equiv, 0, 500, 1);
        spinEquiv = new JSpinner(sm);
        l.setLabelFor(spinEquiv);
        p.add(spinEquiv);

        //Lay out the panel
        SpringUtilities.makeCompactGrid(p,
                                        2, 2, //rows, cols
                                        6, 6,        //initX, initY
                                        6, 6);       //xPad, yPad
        
        general.add(p, BorderLayout.NORTH);
        add(general);
        
    	// Mode modification - afficher les boutons
        JPanel commandBox = new JPanel();
        save = new JButton("Enregistrer");
        save.setActionCommand("Enregistrer");
        save.addActionListener(new PanelStatutListener());
        commandBox.add(save);
        annuler = new JButton("Annuler");
        annuler.setActionCommand("Annuler");
        annuler.addActionListener(new PanelStatutListener());
        commandBox.add(annuler);
        drawCommandBox(commandBox);
                
	}

	
	private class PanelStatutListener implements ActionListener {
		
		public void actionPerformed(ActionEvent e) {
			System.out.println("Action");		
			if (e.getActionCommand().equals("Enregistrer")) {
				System.out.println("Demande d'enregistrement");
				StatutEnseignantBL statutSave = new StatutEnseignantBL();
				ArrayList<String> listeErreurs;
				// si -1, c'est un ajout d'un statut d'enseignant, sinon modification du
				// statut d'enseignant de l'id concerné
				if (id == -1) {
					listeErreurs = statutSave.ajouterStatutEnseignant(getId(), getNom(), getEquiv());
					if (! listeErreurs.isEmpty()) {
						new MessageErrorUI(masterClass, listeErreurs);
					} else {
						dispose();
					}
				} else {
					listeErreurs = statutSave.modifierStatutEnseignant(getId(), getNom(), getEquiv());
					if (! listeErreurs.isEmpty()) {
						new MessageErrorUI(masterClass, listeErreurs);
					} else {
						dispose();
					}
				}
				panelSource.refreshListe();
			} else if (e.getActionCommand().equals("Annuler")) {
				System.out.println("annulation");
				dispose();
			}
		}
	}
}
